package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.News;
import com.example.entity.NewsContent;
import com.example.qo.NewsQuery;
import com.example.redis.service.INewsRedisService;
import com.example.service.INewsContentService;
import com.example.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yunan
 * @since 2023-04-13
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {

    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsContentService newsContentService;
    @Autowired
    private INewsRedisService newsRedisService;

    @RequestMapping("/page")
    public Page<News> page(NewsQuery qo){
        return newsService.queryPage(qo);
    }

    @RequestMapping("/detail")
    public News detail(Long id){
        News news = newsService.getById(id);
        NewsContent content = newsContentService.getById(id);
        news.setContent(content);
        /*
        * 阅读数+1
        * */
        long viewnum = newsRedisService.viewnumIncr(id);
        news.setViewnum(viewnum);
        return news;
    }

    @RequestMapping("/chijiuhua")
    public boolean chijiuhua(){
        return newsRedisService.chijiuhua();
    }

}

